package xconfig

import (
	"context"
	"errors"
	"gf-xb-api/internal/consts"
	"time"

	"github.com/gogf/gf/v2/frame/g"
)

var (
	ctx       = context.Background()
	useApollo bool
)

const (
	// DefaultCaptchaLen ... Default number of digits in captcha solution.
	DefaultCaptchaLen = 6

	// CollectNum ... The number of captchas created that triggers garbage collection used by default store.
	CollectNum = 100

	// DefaultCaptchaExpiration ...  Expiration time of captchas used by default store.
	DefaultCaptchaExpiration = 300 * time.Second

	// DefaultCaptchaStdWidth ... Standard width of a captcha image.
	DefaultCaptchaStdWidth = 240

	// DefaultCaptchaStdHeight ... standard height of a captcha image
	DefaultCaptchaStdHeight = 80
)

func init() {

	userApolloVar, err := g.Config().Get(ctx, consts.SERVER_USEAPOLLO)
	if err != nil {
		panic(err)
	}

	if userApolloVar == nil {
		useApollo = false
	} else {
		useApollo = userApolloVar.Bool()
	}
}

type RedisConfig struct {
	Host     string
	Db       int
	Port     int
	Password string
}

type CaptchaConfig struct {
	Open       bool          // 是否开启验证码
	Len        int           // 验证码长度
	Expiration time.Duration // 验证码有效期
	StdWith    int           // 宽度
	StdHeight  int           // 高度
}

// GetRedisConfig ... 获得Redis配置信息
func GetRedisConfig() (redisConfig *RedisConfig, err error) {

	if useApollo {
		// todo apollo 配置
		return nil, errors.New("under development")
	}

	host := g.Config().MustGet(ctx, consts.REDIS_DEFAULT_ADDRESS).String()
	db := g.Config().MustGet(ctx, consts.REDIS_DEFAULT_DB).Int()
	port := g.Config().MustGet(ctx, consts.REDIS_DEFAULT_PORT).Int()
	password := g.Config().MustGet(ctx, consts.REDIS_DEFAULT_PASS).String()

	redisConfig = &RedisConfig{
		Host:     host,
		Db:       db,
		Port:     port,
		Password: password,
	}

	return redisConfig, nil
}

// GetCaptchaConfig ... 获得验证码配置信息
func GetCaptchaConfig() (captchaConfig *CaptchaConfig, err error) {

	if useApollo {
		// todo
		return nil, errors.New("under development")
	}

	// 是否开启验证码
	open := false
	openVar, err := g.Config().Get(ctx, consts.CAPTCHA_OPEN)
	if err == nil && openVar != nil {
		open = openVar.Bool()
	}

	// 验证码长度
	len := DefaultCaptchaLen
	lenVar, err := g.Config().Get(ctx, consts.CAPTCHA_DIGITLEN)
	if err == nil && lenVar != nil {
		len = lenVar.Int()
	}

	// 验证码过期时间
	expiration := DefaultCaptchaExpiration
	timeoutSecs, err := g.Config().Get(ctx, consts.CAPTCHA_TIMEOUT)
	if err == nil && timeoutSecs != nil {
		expiration = time.Duration(timeoutSecs.Int()) * time.Second
	}

	// 宽度
	stdWidth := DefaultCaptchaStdWidth
	stdWidthVar, err := g.Config().Get(ctx, consts.CAPTCHA_STDWIDTH)
	if err == nil && stdWidthVar != nil {
		stdWidth = stdWidthVar.Int()
	}

	// 高度
	stdHeight := DefaultCaptchaStdHeight
	stdHeightVar, err := g.Config().Get(ctx, consts.CAPTCHA_STDHEITHT)
	if err == nil && stdHeightVar != nil {
		stdHeight = stdHeightVar.Int()
	}

	config := &CaptchaConfig{
		Open:       open,
		Len:        len,
		Expiration: expiration,
		StdWith:    stdWidth,
		StdHeight:  stdHeight,
	}

	return config, nil
}
